<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专业名称" prop="majorName">
        <el-input
          v-model="queryParams.majorName"
          placeholder="请输入专业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:major:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:major:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:major:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:major:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="majorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="专业id" align="center" prop="majorId" v-if="true"/>-->
      <el-table-column label="专业名称" align="center" prop="majorName" />
      <el-table-column label="专业方向" align="center" prop="majorfieldList" >
        <template slot-scope="scope">
            <el-tag 
              v-for="item in scope.row.majorfieldList"
              style="margin-top: 5px"
              :key="item"
              effect="dark">
              {{ item.fieldName }}
            </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:major:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:major:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专业信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="专业名称" prop="majorName">
          <el-input v-model="form.majorName" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="专业方向" prop="majorfieldList">
          <el-table
            :data="form.majorfieldList"
            stripe
            style="margin-top:20px; width: 100%">
            <el-table-column
              prop="fieldCode"
              label="方向编号"
              width="220">
              <template slot-scope="scope">
                <el-input v-model="scope.row.fieldCode"></el-input>
              </template>
            </el-table-column>
            <el-table-column
              prop="fieldName"
              label="方向名称"
              width="220">
              <template slot-scope="scope">
                <el-input v-model="scope.row.fieldName"></el-input>
              </template>
            </el-table-column>
            <el-table-column
              prop="operate"
              label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="handleDeleteExperience(scope.$index, scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddExperienceline">新增专业方向
            </el-button>
          </div>

<!--          <el-button-->
<!--            size="mini"-->
<!--            type="success"-->
<!--            icon="el-icon-save"-->
<!--            @click="handleAddExperienceline">新增-->
<!--          </el-button>-->
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMajor, getMajor, delMajor, addMajor, updateMajor } from "@/api/system/major";
import majorfield from "@/views/system/majorfield/index.vue";

export default {
  name: "Major",
  computed: {
    majorfield() {
      return majorfield
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专业信息表格数据
      majorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        majorName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        majorId: [
          { required: true, message: "专业id不能为空", trigger: "blur" }
        ],
        majorName: [
          { required: true, message: "专业名称不能为空", trigger: "blur" }
        ],
        majorfieldList: [
          { validator: this.validateMajorfieldList, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询专业信息列表 */
    getList() {
      this.loading = true;
      listMajor(this.queryParams).then(response => {
        this.majorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        majorId: undefined,
        majorName: undefined,
        majorfieldList: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.majorId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专业信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const majorId = row.majorId || this.ids
      getMajor(majorId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改专业信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form.majorfieldList)
          this.buttonLoading = true;
          if (this.form.majorId != null) {
            updateMajor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMajor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const majorIds = row.majorId || this.ids;
      this.$modal.confirm('是否确认删除专业信息编号为"' + majorIds + '"的数据项？').then(() => {
        this.loading = true;
        return delMajor(majorIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/major/export', {
        ...this.queryParams
      }, `major_${new Date().getTime()}.xlsx`)
    },
    //增加经验行
    handleAddExperienceline() {
      if (this.form.majorfieldList == undefined) {
        this.form.majorfieldList = new Array();
      }
      let obj = {};
      this.form.majorfieldList.push(obj);
    },
    //删除经验行
    handleDeleteExperience(index) {
      console.log(index);
      if (this.form.majorfieldList.length == 1){
        this.$modal.msgError("最少保留一个专业方向");
        return
      }
      this.form.majorfieldList.splice(index, 1)
    },
    validateMajorfieldList(rule, value, callback) {
      console.log(value)
      if (value == undefined || value.length === 0){
        callback(new Error("专业方向不能为空"));
      }else {
        for (let i = 0; i < value.length; i++) {
          const item = value[i];
          if (!item.fieldCode || !item.fieldName) {
            callback(new Error("专业方向必须填写完整"));
            return;
          }
        }
        callback();
      }
    },
  }
};
</script>
